package com.xxxx.server.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.PositionMapper;
import com.xxxx.server.pojo.Position;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IPositionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    /**
     * 查询所有职位信息
     */
    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<Position> getAllPositions() {
        return positionMapper.getAllPositions() ;
    }

    /**
     * 添加职位
     * @param position
     * @return
     */
    @Override
    public RespBean addPosition(Position position) {
        position.setEnabled(true);
        position.setCreateDate(LocalDateTime.now());
if (null==position.getName()||position.getName()==""){
    return RespBean.error("职位名称不能为空");
}
        if(null!=positionMapper.queryPositionByName(position.getName())){
            return RespBean.error("职位名称已存在,请重新输入");
        }


        if (positionMapper.addPosition(position)) {
            return RespBean.success("添加成功");
        } else {

            return RespBean.error("添加失败");
        }
    }

    /**
     * 修改职位信息
     * @param position
     * @return
     */
    @Override
    public RespBean updatePosition(Position position) {
        if (null==position.getName()||position.getName()==""){
            return RespBean.error("职位名称不能为空");
        }
        if(null!=positionMapper.queryPositionByName(position.getName())){
            return RespBean.error("职位名称已存在,请重新输入");
        }


        if (positionMapper.updatePosition(position)){

            return RespBean.success("更新成功");    }
        else
        {
            System.out.println(position);
            return RespBean.error("更新失败");
        }




    }

    /**
     * 单项删除
     * @param id
     * @return
     */

    @Override
    public RespBean deletePosition(Integer id) {

        System.out.println(id);
        if (positionMapper.deletePosition(id)) {
            return RespBean.success("删除成功");
        } else {
            return RespBean.error("删除失败");
        }

    }

    /**
     * 多项删除
     * @param ids
     * @return
     */
    @Override
    public RespBean deletePositions(String  ids) {

        //成功删除的数据
        int success =0;
        //失败删除的数据
        int error =0;

        //存入数组
        String[] idstr = ids.split(",");

        //将要删除的职位id遍历
        for (int i = 0; i < idstr.length; i++) {
            int num = positionMapper.deleteById(idstr[i]);
            if (num == 1) {
                success++;
            }else {
                error++;
            }
        }
        return RespBean.success("成功删除了"+success+"条数据，有"+error+"条数据在删除失败");


    }
}
